package com.ruoyi.project.simple.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.IdUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.project.simple.domain.SplOrder;
import com.ruoyi.project.simple.domain.SplOrderDetailReport;
import com.ruoyi.project.simple.domain.vo.SplOrderDetailVO;
import com.ruoyi.project.simple.mapper.SplOrderMapper;
import com.ruoyi.project.simple.service.ISplOrderDetailReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.simple.mapper.SplOrderDetailMapper;
import com.ruoyi.project.simple.domain.SplOrderDetail;
import com.ruoyi.project.simple.service.ISplOrderDetailService;

/**
 * 样品订单明细Service业务层处理
 *
 * @author zhujj
 * @date 2020-01-09
 */
@Service
public class SplOrderDetailServiceImpl implements ISplOrderDetailService {

    @Autowired
    private SplOrderMapper splOrderMapper;

    @Autowired
    private SplOrderDetailMapper splOrderDetailMapper;

    @Autowired
    private ISplOrderDetailReportService splOrderDetailReportService;

    /**
     * 查询样品订单明细
     *
     * @param orderDetailId 样品订单明细ID
     * @return 样品订单明细
     */
    @Override
    public SplOrderDetailVO selectSplOrderDetailById(String orderDetailId) {
        return splOrderDetailMapper.selectSplOrderDetailById(orderDetailId);
    }

    /**
     * 查询样品订单明细列表
     *
     * @param splOrderDetail 样品订单明细
     * @return 样品订单明细
     */
    @Override
    public List<SplOrderDetailVO> selectSplOrderDetailList(SplOrderDetail splOrderDetail) {
        return splOrderDetailMapper.selectSplOrderDetailList(splOrderDetail);
    }

    /**
     * 新增样品订单明细
     *
     * @param splOrderDetail 样品订单明细
     * @return 结果
     */
    @Override
    public int insertSplOrderDetail(SplOrderDetail splOrderDetail) {
        splOrderDetail.setOrderDetailId(IdUtils.fastSimpleUUID());
        splOrderDetail.setState(10);
        splOrderDetail.setUpdateBy(SecurityUtils.getUsername());
        splOrderDetail.setCreateBy(SecurityUtils.getUsername());
        splOrderDetail.setCreateTime(DateUtils.getNowDate());
        splOrderDetail.setUpdateTime(DateUtils.getNowDate());
        return splOrderDetailMapper.insertSplOrderDetail(splOrderDetail);
    }

    /**
     * 修改样品订单明细
     *
     * @param splOrderDetail 样品订单明细
     * @return 结果
     */
    @Override
    public int updateSplOrderDetail(SplOrderDetailVO splOrderDetail) {
        splOrderDetail.setUpdateBy(SecurityUtils.getUsername());
        splOrderDetail.setUpdateTime(DateUtils.getNowDate());
        //保存报告
        if (splOrderDetail.getOrder() != null) {
            splOrderDetailReportService.updateSplOrderDetailReport(splOrderDetail.getReport());
        }
        splOrderDetailMapper.updateSplOrderDetail(splOrderDetail);
        //已复核
        if (splOrderDetail.getState() == 40) {
            int noReviewCount = splOrderDetailMapper.selectSplOrderNoReviewList(splOrderDetail.getOrderId());
            if(noReviewCount==0){
                SplOrder splOrder = new SplOrder();
                splOrder.setOrderId(splOrderDetail.getOrderId());
                splOrder.setDelFlag("7");
                splOrderMapper.updateSplOrder(splOrder);
            }
        }
        //已付款
        if (splOrderDetail.getState() == 50) {
            int noPayedCount = splOrderDetailMapper.selectSplOrderNoPayedList(splOrderDetail.getOrderId());
            if(noPayedCount==0){
                SplOrder splOrder = new SplOrder();
                splOrder.setOrderId(splOrderDetail.getOrderId());
                splOrder.setDelFlag("9");
                splOrderMapper.updateSplOrder(splOrder);
            }
        }
        return 1;
    }

    /**
     * 根据样品iD修改
     *
     * @param splOrderDetail
     */
    @Override
    public void updateSplOrderDetailByOrderId(SplOrderDetail splOrderDetail) {
        splOrderDetail.setUpdateTime(DateUtils.getNowDate());
        splOrderDetail.setUpdateBy(SecurityUtils.getUsername());
        splOrderDetailMapper.updateSplOrderDetailByOrderId(splOrderDetail);
    }

    /**
     * 批量删除样品订单明细
     *
     * @param orderDetailIds 需要删除的样品订单明细ID
     * @return 结果
     */
    @Override
    public int deleteSplOrderDetailByIds(String[] orderDetailIds) {
        return splOrderDetailMapper.deleteSplOrderDetailByIds(orderDetailIds);
    }

    /**
     * 删除样品订单明细信息
     *
     * @param orderDetailId 样品订单明细ID
     * @return 结果
     */
    @Override
    public int deleteSplOrderDetailById(String orderDetailId) {
        return splOrderDetailMapper.deleteSplOrderDetailById(orderDetailId);
    }


}
